﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BiffenGUI.BiffenService;

namespace BiffenGUI.UserControls
{
    public partial class BookingManagment : UserControl
    {
        private BiffenServiceClient service = null;
        private List<Booking> found = null;
        private int selectedResult = -1;
        private SearchElement noResults = new SearchElement(null, "Ingen booking blev fundet!");

        public BookingManagment()
        {
            InitializeComponent();
        }

        public void SetServiceReferance(BiffenServiceClient svc)
        {
            service = svc;
            Initialize();
        }

        public void SetFocus()
        {
            txtSearch.Focus();
        }

        private void Initialize()
        {
            listSearch.DisplayMember = "Value";
            listSearch.ValueMember = "BookingId";
            txtSearch.Focus();
            ClearInfoFields();
        }

        private void ClearInfoFields()
        {
            lblPhone.Text = "";
            lblMovie.Text = "";
            lblTime.Text = "";
            lblTheater.Text = "";
            lblSeats.Text = "";
            lblStatus.Text = "";
        }

        private string GetShowTime(DateTime start)
        {
            return start.ToShortDateString() + " " + start.ToShortTimeString();
        }

        private void bSearch_Click(object sender, EventArgs e)
        {
            bSold.Enabled = false;
            bDelete.Enabled = false;

            int phone;
            if (int.TryParse(txtSearch.Text, out phone))
            {
                found = service.FindBookingByPhone(phone);
                listSearch.Items.Clear();
                if (found != null && found.Count > 0)
                {
                    foreach (Booking b in found)
                    {
                        listSearch.Items.Add(new SearchElement(b, b.Show.Movies[0].Title + ": " + b.Show.StartTime.ToString()));
                    }
                }
                else
                {
                    listSearch.Items.Add(noResults);
                }
            }
            else
            {
                MessageBox.Show("Telefon nummer må kun indeholde tal!");
                txtSearch.Text = "";
                txtSearch.Focus();
            }
        }

        private void listSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearInfoFields();
            if (listSearch.SelectedIndex > -1)
            {
                selectedResult = listSearch.SelectedIndex;
                SearchElement se = (SearchElement)listSearch.Items[selectedResult];
                if (se.Booking != null)
                {
                    Booking b = se.Booking;

                    // Enable buttons
                    bSold.Enabled = true;
                    bDelete.Enabled = true;

                    // Update info:
                    lblPhone.Text = b.PhoneNumber.ToString();
                    lblMovie.Text = b.Show.Movies[0].Title;
                    lblTime.Text = b.Show.StartTime.ToString();
                    lblTheater.Text = b.Show.Theater.Id.ToString();
                    string seats = "";
                    foreach (int i in b.Seats)
                    {
                        if (seats != "")
                            seats += ", ";
                        seats += i.ToString();
                    }
                    lblSeats.Text = seats;

                    lblPriceValue.Text = (b.Show.Movies[0].PriceCategory.Price * b.Seats.Count).ToString();

                    string status = "Reserveret";
                    if (b.Status == Booking.States.Sold)
                    {
                        status = "Solgt"; bSold.Enabled = false;
                    }
                    else if (b.Status == Booking.States.Released)
                        status = "Frigivet";
                    lblStatus.Text = status;
                }
            }
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                bSearch_Click(this, e);
            }
            else
            {
                e.SuppressKeyPress = Tools.ValidateKeyPressAsNumber(e);
            }
        }

        private void bSold_Click(object sender, EventArgs e)
        {
            Booking b = ((SearchElement)listSearch.Items[selectedResult]).Booking;
            if (service.MarkBookingAsSold(b.ID) > 0)
            {
                lblStatus.Text = "Solgt";
                b.Status = Booking.States.Sold;
            }
            else
            {
                MessageBox.Show("Kunne ikke markerer bookingen som solgt!");
            }
        }

        private void bDelete_Click(object sender, EventArgs e)
        {
            Booking b = ((SearchElement)listSearch.Items[selectedResult]).Booking;
            if (MessageBox.Show("Vil du slette denne booking?", "Slet?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (service.DeleteBookingById(b.ID) > 0)
                {
                    found.Remove(b);
                    listSearch.Items.RemoveAt(selectedResult);
                    ClearInfoFields();
                    listSearch.SelectedIndex = -1;
                    if (listSearch.Items.Count < 1)
                    {
                        listSearch.Items.Add(noResults);
                    }
                    bSold.Enabled = false;
                    bDelete.Enabled = false;
                }
                else
                {
                    MessageBox.Show("Kunne ikke slette bookingen");
                }
            }
        }

    }


    public class SearchElement
    {
        public Booking Booking { get; set; }
        public string Value { get; set; }
        public SearchElement(Booking b, string val)
        {
            Booking = b;
            Value = val;
        }
    }

}
